package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        Long dishId = shoppingCart.getDishId();//获取菜品id
        //2、构造菜品 | 套餐查询条件
        if(dishId != null){ //如果dishId不等于null说明是菜品数据
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());//3、构建用户查询条件
        ShoppingCart cartInDB = shoppingCartMapper.selectOne(queryWrapper);//4、执行查询条件
        //2、判断对象，有还是没有？
        if(cartInDB == null){   //表明这件菜品|套餐在数据库表里面没有记录，此时要执行添加操作
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id
            shoppingCartMapper.insert(shoppingCart);
            return shoppingCart;
        }else { //套餐在数据库表里面有记录，修改数量
            cartInDB.setNumber(cartInDB.getNumber() + 1);
            shoppingCartMapper.updateById(cartInDB);
            return cartInDB;
        }
    }

    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        Long dishId = shoppingCart.getDishId();//获取菜品id
        //2、构造菜品 | 套餐查询条件
        if(dishId != null){ //如果dishId不等于null说明是菜品数据
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());//3、构建用户查询条件
        ShoppingCart cartInDB = shoppingCartMapper.selectOne(queryWrapper);//4、执行查询条件
        //2、判断对象，有还是没有？
        if(cartInDB.getNumber() < 2){   //表明这件菜品|套餐在数据库表里面没有记录，此时要执行添加操作
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id
            shoppingCartMapper.delete(queryWrapper);
            return shoppingCart;
        }else { //套餐在数据库表里面有记录，修改数量
            cartInDB.setNumber(cartInDB.getNumber() - 1);
            shoppingCartMapper.updateById(cartInDB);
            return cartInDB;
        }
    }

    @Override
    public List<ShoppingCart> list() {
        //1、设置条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        //2、执行查询
        return shoppingCartMapper.selectList(queryWrapper);
    }

    @Override
    public void clean() {
        //1、创建条件对象
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //2、执行删除
        shoppingCartMapper.delete(queryWrapper);
    }
}
